<?php
/**
 * img
 * @author sink <sink.cup@gmail.com>
 */
class ImgController extends BasicController
{
    private $img_model;

    public function __construct()
    {
        parent::__construct();
        $this->img_model = new ImgModel();
    }

    public function router()
    {
        if(0 < preg_match('/\/[0-9a-z]{32}-[0-9]{1,3}-([0-9]+|auto)x([0-9]+|auto)\.(jpg|png)$/', $_SERVER['REQUEST_URI']))
        {
            switch (strtolower($_SERVER['REQUEST_METHOD']))
            {
                case 'get':
                    return $this->get();
                    break;
                case 'delete':
                    return $this->delete();
                    break;
            }
        }
        if(0 < preg_match('/\/$/', $_SERVER['REQUEST_URI']))
        {
            switch (strtolower($_SERVER['REQUEST_METHOD']))
            {
                case 'post':
                    return $this->add();
                    break;
            }
        }
        $error = array
        (
            'code' => '501',
            'msg' => '没有这个功能'
        );
        throw new ControllerException(json_encode($error));
    }

    /**
     * api POST /
     *
     * 添加一张图片
     *
     * demo curl -F 'file=@1.jpg' -F 'need_size={"200xauto","1000xauto"}' -H 'Accept:application/json; version=0.1' 'http://img.shaixuan.org/'
     *
     */
    public function add()
    {
        try
        {
            $tmp = explode('.',$_FILES['file']['name']);
            $last = count($tmp) -1;
            $ext = strtolower($tmp[$last]); //后缀名用小写
            if(!in_array($ext, array('jpg', 'png')))
            {
                $error = array
                (
                    'code' => '202',
                    'msg' => ''
                );
                throw new ControllerException(json_encode($error));
            }
            $filename = md5(file_get_contents($_FILES['file']['tmp_name'])) . '.' . $ext;
            $path = ImgModel::getPath($filename);
            move_uploaded_file($_FILES["file"]["tmp_name"], $path);
            return array
            (
                'result' => array
                (
                    'status' => array
                    (
                        'code' => 200,
                        'msg' => 'OK'
                    ),
                    'data' => array
                    (
                        'filename' => $filename
                    )
                )
            );
        }
        catch (Exception $e)
        {
            $error = array
            (
                'code' => '202',
                'msg' => ''
            );
            throw new ControllerException(json_encode($error));
        }
    }
    
    /**
     * api GET /{id}-{quality}-{width}x{height}.{ext}
     *
     * 取某个分类的属性
     *
     * demo curl 'http://img.shaixuan.org/99469ff033e2760b3a2778338270df18-80-200xauto.jpg'
     *
     */
    public function get()
    {
        try
        {
            //array(3) { [0]=> string(32) "99469ff033e2760b3a2778338270df18" [1]=> string(2) "80" [2]=> string(12) "200xauto.jpg" }
            $want_filename = substr($_SERVER['REQUEST_URI'], 1);
            $tmp = explode('-', $want_filename);
            if($tmp[1] > 100)
            {
                exit;
            }
            self::$content_type = $this->img_model->getFile($want_filename);
            return array
            (
                'result' => array
                (
                    'status' => array
                    (
                        'code' => 200,
                        'msg' => 'OK'
                    ),
                    'data' => array
                    (
                        'file' => ImgModel::getPath($want_filename)
                    )
                )
            );
        }
        catch (Exception $e)
        {
            exit; //退出就是404了。
        }
    }
}
?>
